using System;
using System.Collections.Generic;
using System.Text;
using System.Drawing;
using System.IO;
using Microsoft.Win32;
using System.Runtime.InteropServices;

namespace Bing4Free
{
	class WindowsAPI
	{
		[DllImport("user32.dll")]
		public static extern IntPtr FindWindow(string lpClassName, string lpWindowName);

		[DllImport("User32.dll")]
		public static extern int SetForegroundWindow(IntPtr hWnd);

		[DllImport("wininet.dll")]
		private extern static bool InternetGetConnectedState(out int Description, int ReservedValue);

		public static bool IsConnectedToInternet()
		{
			int Desc;
			return InternetGetConnectedState(out Desc, 0);
		}

		public static bool IsAutoStartup()
		{
			bool isAutoStart = false;

			RegistryKey key = Registry.CurrentUser.OpenSubKey(@"Software\Microsoft\Windows\CurrentVersion\Run", true);

			try
			{
				string tmp = (string)key.GetValue("Bing4Free");
				isAutoStart = !string.IsNullOrEmpty(tmp) ? true : false;
			}
			catch (Exception ex)
			{
				string error = ex.Message;
			}

			return isAutoStart;
		}

		public static DesktopBackgroundStyle GetCurrentDesktopWallpaperBackgroundStyle()
		{
			RegistryKey key = Registry.CurrentUser.OpenSubKey("Control Panel\\Desktop", false);

			int style = Int32.Parse(key.GetValue(@"WallpaperStyle").ToString());
			int tile = Int32.Parse(key.GetValue(@"TileWallpaper").ToString());

			if (style == 0 && tile == 0)
			{
				return DesktopBackgroundStyle.Centered;
			}
			else if (style == 0 && tile == 1)
			{
				return DesktopBackgroundStyle.Tiled;
			}
			else
			{
				return DesktopBackgroundStyle.Stretched;
			}
		}

		/// <summary>
		/// Adds the named item and its path to the Current User
		/// startup in registry.
		/// </summary>
		/// <param name="name"></param>
		/// <param name="path"></param>
		public static void AddStartupItem(string name, string path)
		{
			RegistryKey key = Registry.CurrentUser.OpenSubKey(@"Software\Microsoft\Windows\CurrentVersion\Run", true);

			key.SetValue(name, path);
		}

		/// <summary>
		/// Removes the named item from Current User startup in registry.
		/// </summary>
		/// <param name="name"></param>
		public static void RemoveStartupItem(string name)
		{
			RegistryKey key = Registry.CurrentUser.OpenSubKey(@"Software\Microsoft\Windows\CurrentVersion\Run", true);

			key.DeleteValue(name, false);
		}

		/// <summary>
		/// Set the Desktop background to the supplied image.  Image must be
		/// saved as BMP (saves to My Pictures), and uses the specified
		/// style (Tiled, Centered, Stretched).
		/// </summary>
		/// <param name="img"></param>
		/// <param name="style"></param>
		public static void SetDesktopBackground(Bitmap img, DesktopBackgroundStyle style)
		{
			string picturesPath = Path.Combine(System.Environment.GetFolderPath(Environment.SpecialFolder.MyPictures), "Bing4Free.bmp");

			// Convert to BMP and save)
			img.Save(picturesPath, System.Drawing.Imaging.ImageFormat.Bmp);

			//Write style info to registry
			RegistryKey key = Registry.CurrentUser.OpenSubKey("Control Panel\\Desktop", true);

			switch (style)
			{
				case DesktopBackgroundStyle.Stretched:
					key.SetValue(@"WallpaperStyle", "2");
					key.SetValue(@"TileWallpaper", "0");
					break;

				case DesktopBackgroundStyle.Centered:
					key.SetValue(@"WallpaperStyle", "0");
					key.SetValue(@"TileWallpaper", "0");
					break;

				case DesktopBackgroundStyle.Tiled:
					key.SetValue(@"WallpaperStyle", "0");
					key.SetValue(@"TileWallpaper", "1");
					break;
			}

			SystemParametersInfo(SPI_SETDESKWALLPAPER, 0, picturesPath, SPIF_UPDATEINIFILE | SPIF_SENDWININICHANGE);
		}

		const int SPI_SETDESKWALLPAPER = 20;
		const int SPIF_UPDATEINIFILE = 0x01;
		const int SPIF_SENDWININICHANGE = 0x02;

		[DllImport("user32.dll", CharSet = CharSet.Auto)]
		static extern int SystemParametersInfo(int uAction, int uParam, string lpvParam, int fuWinIni);
	}

	/// <summary>
	/// Enumeration for desktop image background display styles.
	/// </summary>
	public enum DesktopBackgroundStyle : int
	{
		Tiled, Centered, Stretched
	}
}
